package com.le.tester.book.concurrentjavafight.l_14;

import org.omg.CORBA.Object;

/**
 * createTime：2024/5/14 13:55
 * description：有界缓存实现的基类
 */
public abstract class BaseBoundedBuffer<V> {
    private final V[] buf;

    private int tail;

    private int count;

    private int head;

    public BaseBoundedBuffer(int capacity) {
        this.buf = (V[]) new Object[capacity];
    }

    public synchronized final void doPut(V v) {
        buf[tail] = v;
        if (++tail == buf.length) {
            tail = 0;
        }
        ++count;
    }

    public synchronized final V doTake() {
        V v = buf[head];
        buf[head] = null;
        if (++head == buf.length) {
            head = 0;
        }
        --count;
        return v;
    }

    public synchronized final boolean isFull() {
        return count == buf.length;
    }

    public synchronized final boolean isEmpty() {
        return count == 0;
    }
}
